package com.pencilisland.pay.api.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import com.pencilisland.pay.api.bean.constant.GoodsConstant;
import com.pencilisland.pay.api.entity.GoodsEntity;
import com.pencilisland.pay.api.entity.GoodsImgEntity;
import com.pencilisland.pay.api.repository.GoodsImgRepository;
import com.pencilisland.pay.api.repository.GoodsRepository;
import com.pencilisland.pay.api.service.CacheService;

@Component
public class RedisCacheServiceImpl implements CacheService {
	Logger log = LoggerFactory.getLogger(getClass());

	@SuppressWarnings("rawtypes")
	@Autowired
	RedisTemplate redisTemplate;

	static final String KG = "wiselyKeyGenerator";
	static final String GOODS_CACHE = "goodsCache";
	static final String ALL_GOODS_CACHE = "allGoodsCache";

	@Autowired
	GoodsRepository goodsRepo;
	@Autowired
	GoodsImgRepository goodsImgRepo;

	/**
	 * 读取缓存
	 *
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Object get(final String key) {
		Object result = null;
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		result = operations.get(key);
		return result;
	}

	/**
	 * 写入缓存
	 *
	 * @param key
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public boolean set(final String key, Object value, Long expireTime) {
		boolean result = false;
		try {
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	@Cacheable(value = GOODS_CACHE, keyGenerator = KG)
	public GoodsEntity retrieveGoods(String goodsCode) {
		log.info("重新生成商品【{}】缓存", goodsCode);
		GoodsEntity goods = goodsRepo.findByCode(goodsCode);
		if (goods != null) {
			List<GoodsImgEntity> imgDetails = goodsImgRepo.findByGoodsId(goods.getId());
			goods.setImgDetails(imgDetails);
		}
		return goods;
	}

	@Override
	@CacheEvict(value = GOODS_CACHE, keyGenerator = KG)
	public void clearGoods(String goodsCode) {

	}

	@Override
	@Cacheable(value = ALL_GOODS_CACHE, keyGenerator = KG)
	public List<GoodsEntity> retrieveAllGoods() {
		log.info("重新生成商品列表缓存");
		List<GoodsEntity> goodsList = goodsRepo.findByStatusOrderByWeightAscUpdateTimeDesc(GoodsConstant.STATUS_1);
		if(goodsList!=null&&!goodsList.isEmpty()){
			for(GoodsEntity goods:goodsList){
				List<GoodsImgEntity> imgDetails = goodsImgRepo.findByGoodsId(goods.getId());
				goods.setImgDetails(imgDetails);
			}
		}
		return goodsList;
	}

	@Override
	@CacheEvict(value = ALL_GOODS_CACHE, keyGenerator = KG)
	public void clearAllGoods() {
		
	}

}
